package campeonatofutebol.activity;

import java.net.URL;
import java.util.Collection;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView.OnItemClickListener;
import campeonatofutebol.bean.RepositorioDeDados;
import campeonatofutebol.conn.facebook.CompartilharNoticiaActivity;
import campeonatofutebol.util.CarregadorDeFeeds;
import campeonatofutebol.util.CarregadorDeStatus;
import campeonatofutebol.util.CarregandoFeedsTask;

import com.example.campeonatofutebol.R;

public class MessageList extends ListActivity {

	//	private List<Message> messages;
	//	private Map<URL,String> titles;
	//	private int itemSelecionado = -1;
	WebView mywebview;
	private Collection<String> titles = null;
	protected URL mensagemURL;
	protected String messageItem;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		Intent intent = getIntent();
		if(intent != null) {
			if(intent.getBooleanExtra("carregarFavoritos", false))
				CarregadorDeFeeds.getInstance().setTimeAtual(RepositorioDeDados.getInstance().getFavoritos().get(0));
			else if(intent.getAction() != null) {
				String act = intent.getAction();
				if(act.equals("android.intent.action.VIEW")) {
					Uri uri = intent.getData();
					CarregadorDeFeeds.getInstance().setRssAtual(uri.toString());
				}
			}
		}
		setContentView(R.layout.message_list_layout);
		CarregandoFeedsTask c = new CarregandoFeedsTask(MessageList.this);
		c.execute("");

		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(android.widget.AdapterView<?> parent, View view, int position, long id) {
				messageItem = (String) getListView().getItemAtPosition(position);
				mensagemURL = CarregadorDeFeeds.getInstance().getMensagens().get(messageItem);
				openOptionsMenu();

			};
		});

	}

	private void abrirBrowser(String url){
		final String HTTP = "http://";
		final String HTTPS = "https://";
		if (!url.startsWith(HTTP) && !url.startsWith(HTTPS))
		{
			url = HTTP + url;
		}

		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
		startActivity(intent);
	}

	private void compartilharNoticia(final String noticia, final URL noticiaURL) {
		AlertDialog.Builder b = new Builder(this);
		b.setTitle("Compartilhar notícia");
		b.setMessage("Deseja compartilhar essa notícia em seu facebook?");

		b.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				try {
					CarregadorDeStatus.getInstance().setNovoStatus(noticia +"\n\n" + noticiaURL);
					Intent i = new Intent(MessageList.this, CompartilharNoticiaActivity.class);
					startActivity(i);
				} catch (Exception e) {

				}
			}});

		b.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {}
		});

		b.show();
	}

	public Collection<String> getTitles() {
		return titles;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.message_menu, menu);
		return true;
	}

	@Override
	public void openOptionsMenu() {
		// TODO Auto-generated method stub
		super.openOptionsMenu();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.ler_item:
			closeOptionsMenu();
			abrirBrowser(mensagemURL.toString());
		case R.id.compartilhar_item:
			closeOptionsMenu();
			compartilharNoticia(messageItem, mensagemURL);
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			closeOptionsMenu();
			return true;
		}
		return super.onKeyDown(keyCode, event); 
	}
}